package pl.poznan.put.cs.idss.students.coolring.ui.components;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.LinkedList;
import java.util.List;

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import pl.poznan.put.cs.idss.students.coolring.i18n.Messages;
import pl.poznan.put.cs.idss.students.coolring.ui.GeneralWindow;

//TODO wszystkie stałe do properties

public class ColorPicker extends JPanel implements ActionListener {

	private static final long serialVersionUID = -796398851323911359L;

	private Color currentColor = Color.WHITE;

	private LinkedList<Color> colors = new LinkedList<Color>();
	private List<JButton> buttons = new LinkedList<JButton>();

	private int lastColorsMemory = 10;

	private JDialog dialog;

	private GeneralWindow parent;

	private JButton change;

	public ColorPicker(GeneralWindow parent) {

		this.parent = parent;
		setLocation(728, 0);
		setSize(270, 570);
		setBackground(new Color(60, 70, 80));
		setLayout(new GridLayout(14, 1));
		change = new JButton();
		change.setVisible(true);
		change.setBorderPainted(false);
		change.setActionCommand("changeColor");
		change.addActionListener(this);

		Label newColorLabel = new Label(Messages.get("UI.color_get_new_label"));
		newColorLabel.setAlignment(Label.CENTER);
		newColorLabel.setForeground(Color.WHITE);

		add(newColorLabel);

		add(change);

		Label lastColorsLabel = new Label(
				Messages.get("UI.color_last_colors_label"));
		lastColorsLabel.setAlignment(Label.CENTER);
		lastColorsLabel.setForeground(Color.WHITE);

		add(lastColorsLabel);

		for (int i = 0; i < lastColorsMemory; i++) {
			buttons.add(new JButton());
			setEnabled(true);
			buttons.get(i).setBorderPainted(false);
			colors.add(Color.WHITE);
			add(buttons.get(i));
			final int colorID = i;
			buttons.get(i).setAction(new AbstractAction() {

				private static final long serialVersionUID = 7143137957719655715L;

				public void actionPerformed(ActionEvent e) {
					setCurrentColor(colors.get(colorID));
					repaint();
				}
			});
		}

		repaint();
	}

	public void addAsLastColor(Color c) {
		colors.addFirst(c);
		colors.removeLast();
		repaint();
	}

	@Override
	public void paint(Graphics g) {
		super.paint(g);
		change.setBackground(currentColor);
		for (int i = 0; i < lastColorsMemory; i++) {
			buttons.get(i).setBackground(colors.get(i));
		}
	}

	public Color getCurrentColor() {
		return currentColor;
	}

	public void setCurrentColor(Color currentColor) {
		this.currentColor = currentColor;
	}

	public void actionPerformed(ActionEvent e) {
		new ColorPickerDialog(this);
	}

}
